/* MachinePlayer.java */

package player;

import board.Board;

/**
 * An implementation of an automatic Network player. Keeps track of moves made
 * by both players. Can select a move for itself.
 */
public class MachinePlayer extends Player {

	public static final int DEPTH_DEFAULT = 4;

	private int color; // player's color
	private int opp_color; // opponen'ts color
	private int searchDepth;
	private Board board; // internal representation of board

	// Creates a machine player with the given color. Color is either 0 (black)
	// or 1 (white). (White has the first move.)
	public MachinePlayer(int color) {
		this(color, DEPTH_DEFAULT);
	}

	// Creates a machine player with the given color and search depth. Color is
	// either 0 (black) or 1 (white). (White has the first move.)
	public MachinePlayer(int color, int searchDepth) {
		this.color = color;
		opp_color = (color + 1) % 2; // opponent's color is opposite of player's
										// color
		this.searchDepth = searchDepth;
		board = new Board();
	}

	// Returns a new move by "this" player. Internally records the move (updates
	// the internal game board) as a move by "this" player.
	public Move chooseMove() {
		Move m = Minimax2.chooseMove(board, color, searchDepth);
		board.makeMove(m, color);
		return m;
	}

	// If the Move m is legal, records the move as a move by the opponent
	// (updates the internal game board) and returns true. If the move is
	// illegal, returns false without modifying the internal state of "this"
	// player. This method allows your opponents to inform you of their moves.
	public boolean opponentMove(Move m) {
		if (board.isLegal(m, opp_color)) {
			board.makeMove(m, opp_color);
			return true;
		} else
			return false;
	}

	// If the Move m is legal, records the move as a move by "this" player
	// (updates the internal game board) and returns true. If the move is
	// illegal, returns false without modifying the internal state of "this"
	// player. This method is used to help set up "Network problems" for your
	// player to solve.
	public boolean forceMove(Move m) {
		if (board.isLegal(m, color)) {
			board.makeMove(m, color);
			return true;
		} else
			return false;
	}

}
